/**
 * @param {number[]} nums
 * @return {number[][]}
 */
var threeSum = function (nums) {
  let result = [], right, left;
  let arr = nums.sort(function(a, b){return a-b})

  for (let i = 0; i < arr.length - 2; i++) {
    right = arr.length - 1
    left = i + 1
    if (i > 0 && arr[i] == arr[i - 1]) {
      continue
    }

    while (right > left) {
      if (arr[i] + arr[right] + arr[left] > 0) {
        right--
      } else if (arr[i] + arr[right] + arr[left] < 0) {
        left++
      } else if (arr[i] + arr[right] + arr[left] == 0) {
        result.push([arr[i], arr[right], arr[left]])
        while (right > left && nums[right] == nums[right - 1]) right--;
        while (right > left && nums[left] == nums[left + 1]) left++;
        right--
        left++
      }
    }
  }

  return result
};

let nums = [-1, 0, 1, 2, -1, -4, -2, -3, 3, 0, 4]
console.log(threeSum(nums))